// src/app.js
const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const cors = require('cors');
const patientsRoutes = require('./routes/patientsRoutes');
const path = require('path'); // 导入 path 模块
app.use(express.json());
// 使用 CORS 中间件
app.use(cors());

// 提供静态文件
// app.use(express.static('www2'));
app.use(express.static(path.join(__dirname, '../www')));


app.get('/', function(req, res) {
  console.log(`enter / method`);
  console.log(path.join(__dirname));
  res.sendFile(path.join(__dirname + '/../www/main.html'));
});

app.use('/api/', patientsRoutes);


// 登录接口
app.post('/api/login', (req, res) => {
  console.log(`enter /login method`);
  const { DEPT_CODE, DEPT_NAME, DOCTOR_CODE, DOCTOR_NAME } = req.body;
  const user = {
    DEPT_CODE,
    DEPT_NAME,
    DOCTOR_CODE,
    DOCTOR_NAME,
  };
  // 这里应该有用户验证的逻辑，这里简化处理
  if (true) {
    res.status(200).json({ success: true,data:user});
  } else {
    // 登录失败，返回登录页
    res.status(200).json({ success: false, message: '登陆验证失败!' });
  }
});
var port = 8080;
app.listen(port, () => {
  console.log(`Server is running at http://localhost:${port}`);
});